<template>
	<view class="mine-page">
		<view class="top-banner">
			<image class="banner-bg" src="/static/banner-bg.png" mode="widthFix" />
			<view class="banner-content">
				<view class="avatar-container" @click="isLogin && chooseAvatar()">
					<image :src="isLogin ? userAvatar : defaultAvatar" class="banner-avatar" />
					<button class="avatar-btn" v-if="isLogin">更换头像</button>
				</view>

				<view class="user-info-wrap" v-if="isLogin">
					<view class="banner-info">
						<view class="welcome-text">HI~ 欢迎回来</view>
						<view class="username">{{ userInfo.nickname }}</view> <!-- 改为显示昵称 -->
						<view class="stats-wrap">
							<view class="stats">
								<text>关注{{ followCount }}</text>
								<text class="divider">|</text>
								<text>话题{{ topicCount }}</text>
							</view>
							<button class="logout-btn" @click="logout">退出</button>
						</view>
					</view>
				</view>

				<view class="banner-info unlogin-info" v-else>
					<view class="welcome-text">HI~ 欢迎回来</view>
					<button class="login-btn" @click="goLogin">登录</button>
				</view>
			</view>
		</view>

		<view class="func-icons">
			<view class="icon-item" @click="checkLogin(goCollect)">
				<image src="/static/collection.png" class="icon-img" />
				<text class="icon-text">收藏</text>
			</view>
			<view class="icon-item" @click="checkLogin(goHistory)">
				<image src="/static/history.png" class="icon-img" />
				<text class="icon-text">历史</text>
			</view>
			<view class="icon-item" @click="checkLogin(goSetting)">
				<image src="/static/setting.png" class="icon-img" />
				<text class="icon-text">设置</text>
			</view>
		</view>

		<view class="ad-banner" @click="checkLogin(goPublish)">
			<image class="ad-img" src="/static/ad-banner.png" mode="widthFix" />
			<view class="ad-tag">广告</view>
		</view>

		<view class="bottom-func" :class="{ disabled: !isLogin }">
			<view class="func-card" @click="checkLogin(goPublish)">
				<image src="/static/photo.png" class="func-icon" />
				<text class="func-text">发布图片</text>
			</view>
			<view class="func-card" @click="checkLogin(goPublish)">
				<image src="/static/video.png" class="func-icon" />
				<text class="func-text">发布视频</text>
			</view>
			<view class="func-card" @click="checkLogin(goShare)">
				<image src="/static/share.png" class="func-icon" />
				<text class="func-text">分享链接</text>
			</view>
		</view>

		<view class="footer-space"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLogin: uni.getStorageSync("isLogin") || false,
				userInfo: uni.getStorageSync("userInfo") || {}, // 存储完整用户信息（含昵称）
				defaultAvatar: "/static/photodefault.png",
				userAvatar: "",
				followCount: 3,
				topicCount: 0
			};
		},
		onShow() {
			const currentIsLogin = uni.getStorageSync("isLogin") || false;
			if (this.isLogin !== currentIsLogin) {
				this.isLogin = currentIsLogin;
				this.userInfo = uni.getStorageSync("userInfo") || {}; // 读取用户信息（含昵称）
				uni.setTabBarItem({
					index: 3,
					text: this.isLogin ? "我的" : "未登录"
				});
			}
			if (this.isLogin) {
				const userId = this.userInfo._id || this.userInfo.name; // 用云端用户ID或用户名作为唯一标识
				this.userAvatar = uni.getStorageSync(`avatar_${userId}`) || this.defaultAvatar;
			} else {
				this.userAvatar = this.defaultAvatar;
			}
		},
		methods: {
			goLogin() {
				uni.navigateTo({
					url: "/pages/tabBar/tabcompage/login/login"
				});
			},
			goRegister() {
				uni.navigateTo({
					url: "/pages/tabBar/tabcompage/login/login",
					success: (res) => res.eventChannel.emit("switchToRegister")
				});
			},
			goCollect() {
				uni.navigateTo({
					url: "/pages/UserInfoPage/collect"
				});
			},
			goHistory() {
				uni.navigateTo({
					url: "/pages/UserInfoPage/history"
				});
			},
			goSetting() {
				uni.navigateTo({
					url: "/pages/UserInfoPage/setting"
				});
			},
			goPublish() {
				uni.showToast({
					title: "发布功能开发中",
					icon: "none"
				});
			},
			goShare() {
				uni.showToast({
					title: "分享功能开发中",
					icon: "none"
				});
			},
			checkLogin(callback) {
				if (!this.isLogin) {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					});
					setTimeout(() => this.goLogin(), 1000);
					return;
				}
				callback();
			},
			logout() {
				uni.showModal({
					title: "提示",
					content: "确定退出登录吗？",
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync("isLogin");
							uni.removeStorageSync("userInfo");
							const userId = this.userInfo._id || this.userInfo.name;
							uni.removeStorageSync(`avatar_${userId}`);
							this.isLogin = false;
							this.userInfo = {};
							this.userAvatar = this.defaultAvatar;
							uni.setTabBarItem({
								index: 3,
								text: "未登录"
							});
							uni.showToast({
								title: "退出成功",
								icon: "success"
							});
						}
					}
				});
			},
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ["original", "compressed"],
					sourceType: ["album"],
					success: (res) => {
						const tempPath = res.tempFilePaths[0];
						this.userAvatar = tempPath;
						const userId = this.userInfo._id || this.userInfo.name;
						uni.setStorageSync(`avatar_${userId}`, tempPath);
						uni.showToast({
							title: "头像更换成功",
							icon: "success"
						});
					},
					fail: () => {
						uni.showToast({
							title: "选择头像失败",
							icon: "none"
						});
					}
				});
			}
		}
	};
</script>

<style scoped>
	.mine-page {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding-bottom: 20rpx;
	}

	.top-banner {
		position: relative;
		width: 100%;
		height: 280rpx;
		overflow: hidden;
		border-radius: 0 0 30rpx 30rpx;
	}

	.banner-bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		z-index: 1;
		filter: brightness(0.7);
	}

	.banner-content {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 2;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		color: #fff;
	}

	.avatar-container {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-right: 30rpx;
	}

	.banner-avatar {
		width: 140rpx;
		height: 140rpx;
		border-radius: 50%;
		border: 4rpx solid #fff;
		object-fit: cover;
	}

	.avatar-btn {
		background-color: rgba(0, 0, 0, 0.4);
		color: #fff;
		border: none;
		padding: 5rpx 15rpx;
		border-radius: 15rpx;
		font-size: 22rpx;
		margin-top: 10rpx;
		min-width: auto;
	}

	.user-info-wrap {
		flex: 1;
	}

	.banner-info {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.welcome-text {
		font-size: 32rpx;
		margin-bottom: 10rpx;
		opacity: 0.9;
	}

	.username {
		font-size: 40rpx;
		font-weight: bold;
	}

	.stats-wrap {
		display: flex;
		align-items: center;
		gap: 20rpx;
		margin-top: 10rpx;
	}

	.stats {
		font-size: 28rpx;
		opacity: 0.9;
		display: flex;
		gap: 15rpx;
	}

	.divider {
		opacity: 0.7;
		font-size: 28rpx;
	}

	.logout-btn {
		background-color: rgba(255, 255, 255, 0.25);
		color: #fff;
		border: none;
		padding: 8rpx 20rpx;
		border-radius: 20rpx;
		font-size: 26rpx;
	}

	.unlogin-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.login-btn {
		background-color: #FF5722;
		color: #fff;
		border: none;
		padding: 15rpx 40rpx;
		border-radius: 30rpx;
		font-size: 30rpx;
		width: fit-content;
	}

	.func-icons {
		display: flex;
		justify-content: space-around;
		background-color: #fff;
		padding: 30rpx 0;
		margin: 30rpx 20rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.03);
	}

	.icon-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 15rpx;
		cursor: pointer;
	}

	.icon-img {
		width: 60rpx;
		height: 60rpx;
		object-fit: contain;
	}

	.icon-text {
		font-size: 28rpx;
		color: #333;
	}

	.ad-banner {
		position: relative;
		margin: 0 20rpx 25rpx;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.05);
		cursor: pointer;
	}

	.ad-img {
		width: 100%;
		height: auto;
	}

	.ad-tag {
		position: absolute;
		top: 10rpx;
		right: 10rpx;
		background-color: rgba(0, 0, 0, 0.3);
		color: #fff;
		font-size: 18rpx;
		padding: 2rpx 10rpx;
		border-radius: 10rpx;
	}

	.bottom-func {
		display: flex;
		justify-content: space-around;
		background-color: #fff;
		padding: 35rpx 0;
		margin: 0 20rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.03);
	}

	.func-card {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 15rpx;
		cursor: pointer;
	}

	.func-icon {
		width: 60rpx;
		height: 60rpx;
		object-fit: contain;
	}

	.func-text {
		font-size: 24rpx;
		color: #666;
	}

	.disabled .func-card {
		opacity: 0.4;
		pointer-events: none;
	}

	.footer-space {
		height: 30rpx;
	}
</style>